package com.xiaoy.core.email.service;



import java.io.IOException;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoy.core.email.entity.Email;
import com.xiaoy.util.format.FileUtil;
import com.xiaoy.util.store.VariableStore;

/**
 * 
 * @author chenty 创建时间 2012-7-3
 */

@Service
@Transactional
public class EmailSerivce{

	private static Logger	logger	= Logger.getLogger(EmailSerivce.class);

	public boolean send(Email email) {

		return sendEmail(email);
	}

	/**
	 * 发送邮件
	 * @param emails
	 * @return
	 */
	public boolean[] sends(List<Email> emails) {

		boolean bool[] = new boolean[emails.size()];
		for (int i = 0; i < emails.size(); i++) {
			Email email = emails.get(i);
			bool[i] = send(email);
		}
		return bool;
	}

	/**
	 * 发送邮件
	 * @param email
	 * @return
	 */
	private boolean sendEmail(Email email) {

		MultiPartEmail simpleEmail = null;

		if ("html".equals(sign)) {
			simpleEmail = new HtmlEmail();

			HtmlEmail htmlEmail = new HtmlEmail();
		} else {
			simpleEmail = new MultiPartEmail();
		}

		// 处理附件
		try {
			List<EmailAttachment> emailAttachments = email.getAttachment();
			for (EmailAttachment emailAttachment : emailAttachments) {

				simpleEmail.attach(emailAttachment);
			}
		} catch (EmailException e) {
			logger.debug("邮件发送失败："+e);
//			e.printStackTrace();
		}

		// HtmlEmail simpleEmail = new HtmlEmail ();
		// SimpleEmail simpleEmail2 ;

		if (!"".equals(charset)) {
			simpleEmail.setCharset(charset);
		}

		int portInt = 0;
		try {
			if (!"".equals(email.getProt())) {
				portInt = Integer.parseInt(email.getProt());
			} else {
				portInt = Integer.parseInt(port);
			}
		} catch (Exception e) {
			portInt = 25;
		}
		simpleEmail.setSmtpPort(portInt);

		if (!"".equals(email.getHostName())) {
			simpleEmail.setHostName(email.getHostName());
		} else {
			simpleEmail.setHostName(hostName);
		}

		String from_name = ""; // 发件人
		String from_nick = ""; // 发件人的别称
		String from_mail = ""; // 发件人 邮箱
		String password = "";
		String to_name = ""; // 收件人
		String to_nick = ""; // 收件人的别称
		String cc_name = ""; // 抄送人
		String cc_nick = ""; // 抄送人的别称
		String bcc_name = ""; // 密送人
		String bbc_nick = ""; // 密送人的别称

		// ---
		try {

			if (!"".equals(email.getFrom_mail())) {
				from_mail = email.getFrom_mail();
			} else {
				from_mail = username;
			}

			if (!"".equals(email.getPassword())) {
				password = email.getPassword();
			} else {
				password = EmailSerivce.password;
			}

			if (!"".equals(email.getTo_name())) {
				to_name = email.getTo_name();
			} else {
				to_name = to_nick;
			}

			simpleEmail.setAuthentication(from_mail, password);

			if ("".equals(from_name)) {
				from_name = from_mail;
			}
			simpleEmail.setFrom(from_mail, from_name);

			simpleEmail.addTo(email.getTo_mail(), to_name);

			if (!"".equals(email.getBcc_mail())) {

			}

			if (!"".equals(email.getCc_mail())) {
				simpleEmail.addCc(email.getCc_mail(), email.getCc_name());
			}
			simpleEmail.setSubject(email.getSubject());

			String content = email.getContent();
			if ("".equals(content)) {
				try {
					content = FileUtil.read(template);

				} catch (IOException e) {
					logger.error("发送邮件的时候：在" + template + "没有找到模板文件！");
//					e.printStackTrace();
				}
			}
			
			content = content.replaceAll("\\$\\{from_name\\}", from_name);
			content = content.replaceAll("\\$\\{from_mail\\}", from_mail);
			content = content.replaceAll("\\$\\{from_nick\\}", from_nick);
			
			content = content.replaceAll("\\$\\{date\\}", email.getCreateDate());
			content = content.replaceAll("\\$\\{to_nick\\}", email.getTo_nick());
			content = content.replaceAll("\\$\\{to_mail\\}", email.getTo_mail());

			
			content = content.replaceAll("\\$\\{to_workspace_url\\}", to_workspace_url);

			content = content.replaceAll("\\$\\{to_name\\}", to_name);

			content = content.replaceAll("\\$\\{subject\\}", email.getSubject());

			simpleEmail.setMsg(content);
			simpleEmail.send();
			return true;
		} catch (EmailException e) {
			e.printStackTrace();
		}
		return false;
	}

	private static String	charset				= VariableStore.getValue("mail.charset");			// 字符
	private static String	port				= VariableStore.getValue("mail.port");				// 端口
	private static String	hostName			= VariableStore.getValue("mail.hostName");			//
	private static String	username			= VariableStore.getValue("mail.username");			// portal@uaes.com
	private static String	password			= VariableStore.getValue("mail.password");			// uaes,123
	private static String	from_nick			= VariableStore.getValue("mail.from_nick");
	private static String	template			= VariableStore.getValue("mail.template");
	private static String	to_workspace_url	= VariableStore.getValue("mail.to_workspace_url");

	private static String	sign				= VariableStore.getValue("mail.sign");

}
